# pip install flask
from flask import Flask, request, session, redirect

def auth():
    print(f"auth before request 拦截器执行了，拦截的路径是： {request.path} ")

    # 所有静态文件/static开头的请求，不拦截，不然会导致样式加载不出来
    if request.path.startswith('/static'):
        return

    if request.path == "/login":
        # 继续向后执行，不拦截
        return
    user_info = session.get("user_info")
    if user_info:
        # 已经登录，继续向后执行，不拦截
        return

    # 否则，重定向到登录页面
    return redirect("/login")


def create_app(test_config=None):
    app = Flask(__name__)

    app.secret_key = 'asdfasdfasdf'

    # 将视图对象注册到蓝图中
    from .views import account
    from .views import order
    app.register_blueprint(account.ac)
    app.register_blueprint(order.od)

    app.before_request(auth)

    return app